package com.itheima.users.ui;

import com.itheima.chap04.Result;
import com.itheima.users.entity.User;
import com.itheima.users.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserGUI implements UserUI {
    private UserService userService;
    private JFrame frame;

    public UserGUI(UserService userService) {
        this.userService = userService;
        initialize();
    }

    private void initialize() {
        frame = new JFrame("好朋友管理系统");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLayout(new BorderLayout());

        JLabel welcomeLabel = new JLabel("欢迎使用用户管理系统", SwingConstants.CENTER);
        JButton registerButton = new JButton("注册");
        JButton loginButton = new JButton("登录");
        JButton exitButton = new JButton("退出");

        JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 10, 10));
        buttonPanel.add(registerButton);
        buttonPanel.add(loginButton);
        buttonPanel.add(exitButton);

        frame.add(welcomeLabel, BorderLayout.NORTH);
        frame.add(buttonPanel, BorderLayout.CENTER);

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRegisterDialog();
            }
        });

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showLoginDialog();
            }
        });

        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "谢谢使用！");
                System.exit(0);
            }
        });
    }

    private void showRegisterDialog() {
        JTextField usernameField = new JTextField();
        JPasswordField passwordField = new JPasswordField();
        JPasswordField confirmPasswordField = new JPasswordField();

        Object[] message = {
                "用户名:", usernameField,
                "密码:", passwordField,
                "确认密码:", confirmPasswordField
        };

        int option = JOptionPane.showConfirmDialog(frame, message, "注册", JOptionPane.OK_CANCEL_OPTION);
        if (option == JOptionPane.OK_OPTION) {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());
            String confirmPassword = new String(confirmPasswordField.getPassword());

            if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
                JOptionPane.showMessageDialog(frame, "输入不能为空！");
                return;
            }

            if (!password.equals(confirmPassword)) {
                JOptionPane.showMessageDialog(frame, "两次密码不一致！");
                return;
            }

            Result result = userService.register(username, password);
            if (result.issuccess()) {
                User user = (User) result.getData();
                JOptionPane.showMessageDialog(frame,
                        "注册成功！\n用户ID：" + user.getId() + "\n用户名：" + user.getUsername());
            } else {
                JOptionPane.showMessageDialog(frame, "注册失败：" + result.getMsg());
            }
        }
    }

    private void showLoginDialog() {
        JTextField usernameField = new JTextField();
        JPasswordField passwordField = new JPasswordField();

        Object[] message = {
                "用户名:", usernameField,
                "密码:", passwordField
        };

        int option = JOptionPane.showConfirmDialog(frame, message, "登录", JOptionPane.OK_CANCEL_OPTION);
        if (option == JOptionPane.OK_OPTION) {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());

            if (username.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(frame, "用户名或密码不能为空！");
                return;
            }

            Result result = userService.login(username, password);
            if (result.issuccess()) {
                User user = (User) result.getData();
                JOptionPane.showMessageDialog(frame, "登录成功！\n欢迎回来，" + user.getUsername());
            } else {
                JOptionPane.showMessageDialog(frame, "登录失败：" + result.getMsg());
            }
        }
    }

    @Override
    public void entrance() {
        frame.setVisible(true);
    }
}
